/*
 * $Id: Submit.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.core.api;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.p4eclipse.core.api.ClientUser;
import net.sourceforge.p4eclipse.core.api.IPerforceCommand;
import net.sourceforge.p4eclipse.core.api.PerforceStatus;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 change' command.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Submit extends ClientUser implements IPerforceCommand
{
	private MultiStatus status;
	private String changeSpec;
	private List affectedFiles = new ArrayList();

	/**
	  * Convenience method for creating instances of Change.
	  */
	public static final Submit newInstanceForOutput(String changeSpec)
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Submit(status, changeSpec);
	}
	
	/**
	  * Creates a new instance.
	  */
	private Submit(MultiStatus status, String changeSpec)
	{
		super(status);
		this.status = status;
		this.changeSpec = changeSpec;
	}

	/**
	  * Creates a new instance.
	  */
	private Submit(MultiStatus status)
	{
		super(status);
		this.status = status;
	}

	/** @see IClientUser#inputData() */
	public String inputData()
	{
		return changeSpec;
	}

	/**  @see IClientUser#outputInfo(char, String) */
	public void outputInfo(char level, String infoString)
	{
		IStatus tempStatus  = 
			PerforceStatus.decode(infoString, IStatus.INFO);
			
		if (tempStatus.getCode() == PerforceStatus.REFRESHING.getCode())
		{
			String affectedFile = 
				infoString.substring(0, infoString.lastIndexOf('#'));

			affectedFiles.add(affectedFile);
		}
			
		status.merge(tempStatus);
	}

	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "submit"; //$NON-NLS-1$
	}
	
	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return status;
	}

	/** @return list of files that were altered during submit. */
	public String[] getAffectedFiles()
	{
		return (String[])affectedFiles.toArray(new String[affectedFiles.size()]);	
	}

	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}
}
